#include <stdio.h>
#include "view.h"

#define THRESHOLD_DISPLAY_INSECT 2

cachedImage imageCache[NODE_NUMBER];

//int *mapPosToIndex;

void maxPos(CellPosition position, CellPosition *maxPosition)
{
	if (position.x > maxPosition->x) maxPosition->x = position.x;
	if (position.y > maxPosition->y) maxPosition->y = position.y;
}

ColorMap colorTable[] = {{20,GRAY1},{50,GRAY2},{100,BLACK1},{150,BLACK2},{200,ORANGE1},{250, ORANGE2},{300, RED1},{350, RED2}};

unsigned long chosingColor(int value, ColorMap color_table[])
{
	unsigned long color;
	
	color = color_table[NUM_COLOR - 1].color;

	for (int i = 0; i < NUM_COLOR; i++)	
	{
	    if (value >= color_table[i].thresholdValue)
	    {
		color = color_table[i].color;
		break;
	    }
	}	
	
	return color;
}
/** 
*Date: 04-05-2015
*Desciption: This function will display Sensor network on the screen. The color represents one fo two states: NORMAL, URGENT
*
*/
void showCellNetwork(CellArray  myCell, Display *display, Colormap colormap, Window win, GC gc, int plotSize, int state, int range_sensing, int range_communication){
	CellPosition position;
   	CellPosition maxPosition= {0,0};
   	CellImage image;
   	ImageExtent extent;
	char  colorName[50];
   	XColor color;
   	int xOffset, yOffset, pixelIndex;// offset;
	int depth;
   	struct RGBPixel pixel;
	Pixmap doubleBuffer;
        position = myCell.position; /* x,y offset in the view */
        maxPos(position, &maxPosition);
        image = myCell.image;
        extent = image.extent; /* width and height of the cell */
	depth = DefaultDepth(display, DefaultScreen(display));
	doubleBuffer = XCreatePixmap(display, 
				DefaultRootWindow(display), 
				extent.width * plotSize, 
				extent.height * plotSize, 
				depth);
        for (yOffset = 0; yOffset < extent.height ; yOffset ++)
            for (xOffset = 0; xOffset < extent.width ; xOffset ++)
            {
		if (state == SENSOR_NET_NORMAL)
		{
	 		pixelIndex = yOffset * extent.width + xOffset;
                	pixel = image.pixelArray[pixelIndex];
                	sprintf(colorName, "#%2.2x%2.2x%2.2x", pixel.red, pixel.green, pixel.blue);
                	XParseColor(display, colormap, colorName, &color);
                	XAllocColor(display, colormap, &color);
                	XSetForeground(display, gc, color.pixel);
		}
		else//URGENT state
		{
			XSetForeground(display, gc, COLOR_URGENT);
		}
                XFillRectangle(display, doubleBuffer, gc, 
			   plotSize * xOffset, 
			   plotSize * yOffset , 
			   plotSize, 
			   plotSize);
             }

	int width, height, radius_width, radius_height;
	//position = NetCells[i].position;
	//image = NetCells[i].image;
	height = image.extent.height;
	width = image.extent.width;

	//CellPosition position;
	//CellImage image;

        ///Range communication
	radius_width = plotSize * width * range_communication;
	radius_height = plotSize * height * range_communication;
	XDrawArc(display, win, gc, (position.x * plotSize * width) - (radius_width / 2), 
			(position.y * plotSize * height) - (radius_height / 2), 
			radius_width, radius_height, 360*64, 360*64);
	///Range sensing
	radius_width = plotSize * width * range_sensing;
	radius_height = plotSize * height * range_sensing;
	XDrawArc(display, win, gc, (position.x * plotSize * width) - (radius_width / 2), 
			(position.y * plotSize * height) - (radius_height / 2), 
			radius_width, radius_height, 360*64, 360*64);

	XCopyArea(display, doubleBuffer, win, gc, 0, 0, plotSize * extent.width, plotSize * extent.height, 
			plotSize * (position.x * extent.width), plotSize * (position.y * extent.height));		

}
/** 
*Date: 22-04-2015
*Desciption: This function displays one cell with certain color on picture. The color represents one of three states: NORMAL, FIRED, or ASH. 
*
*/

void showCellForest(CellArray  myCell, Display *display, Colormap colormap, Window win, GC gc, int plotSize, int state)
{
	CellPosition position;
   	CellPosition maxPosition= {0,0};
   	CellImage image;
   	ImageExtent extent;
	char  colorName[50];
   	XColor color;
	unsigned long cellColor;
   	int xOffset, yOffset, pixelIndex;// offset;
	int depth;
   	struct RGBPixel pixel;
	Pixmap doubleBuffer;
        position = myCell.position; /* x,y offset in the view */
        maxPos(position, &maxPosition);
        image = myCell.image;
        extent = image.extent; /* width and height of the cell */
	depth = DefaultDepth(display, DefaultScreen(display));
	doubleBuffer = XCreatePixmap(display, 
				DefaultRootWindow(display), 
				extent.width * plotSize, 
				extent.height * plotSize, 
				depth);
        for (yOffset = 0; yOffset < extent.height ; yOffset ++)
            for (xOffset = 0; xOffset < extent.width ; xOffset ++)
            {
		if (state == NORMAL)
		{
	 		pixelIndex = yOffset * extent.width + xOffset;
                	pixel = image.pixelArray[pixelIndex];
                	sprintf(colorName, "#%2.2x%2.2x%2.2x", pixel.red, pixel.green, pixel.blue);
                	XParseColor(display, colormap, colorName, &color);
                	XAllocColor(display, colormap, &color);
                	XSetForeground(display, gc, color.pixel);
		}
		else
		{
			if (state == FIRED)
	   	  	{
			    cellColor = COLOR_FIRED;
			}
			else if (state == ASH)
			{
			    cellColor = COLOR_ASH;
			}else if (state == EMPTY)
			{
			    cellColor = COLOR_EMPTY;
			}
			XSetForeground(display, gc, cellColor);
		}
                XFillRectangle(display, doubleBuffer, gc, 
			   plotSize * xOffset, 
			   plotSize * yOffset , 
			   plotSize, 
			   plotSize);
             }

	XCopyArea(display, doubleBuffer, win, gc, 0, 0, plotSize * extent.width, plotSize * extent.height, 
			plotSize * (position.x * extent.width), plotSize * (position.y * extent.height));		
}


/** 
*Date: 24-03-2015
*Desciption: This function displays one cell with certain color on picture. The color depends on the number of insects of each cell. 
*/
void showCellInsect(CellArray  myCell, Display *display, Colormap colormap, Window win, GC gc, int plotSize, int nb_insects)
{
	CellPosition position;
   	CellPosition maxPosition= {0,0};
   	CellImage image;
   	ImageExtent extent;
	char  colorName[50];
   	XColor color;
	unsigned long cellColor;
   	int xOffset, yOffset, pixelIndex;// offset;
	int depth;
   	struct RGBPixel pixel;
	Pixmap doubleBuffer;
        position = myCell.position; /* x,y offset in the view */
        maxPos(position, &maxPosition);
        image = myCell.image;
        extent = image.extent; /* width and height of the cell */
	depth = DefaultDepth(display, DefaultScreen(display));
	doubleBuffer = XCreatePixmap(display, 
				DefaultRootWindow(display), 
				extent.width * plotSize, 
				extent.height * plotSize, 
				depth);
        for (yOffset = 0; yOffset < extent.height ; yOffset ++)
            for (xOffset = 0; xOffset < extent.width ; xOffset ++)
            {
		if (nb_insects < THRESHOLD_DISPLAY_INSECT)
		{
	 		pixelIndex = yOffset * extent.width + xOffset;
                	pixel = image.pixelArray[pixelIndex];
                	sprintf(colorName, "#%2.2x%2.2x%2.2x", pixel.red, pixel.green, pixel.blue);
                	XParseColor(display, colormap, colorName, &color);
                	XAllocColor(display, colormap, &color);
                	XSetForeground(display, gc, color.pixel);
		}
		else
		{
			cellColor = chosingColor(nb_insects, colorTable);
	   	  	XSetForeground(display, gc, cellColor);
		}
                XFillRectangle(display, doubleBuffer, gc, 
			   plotSize * xOffset, 
			   plotSize * yOffset , 
			   plotSize, 
			   plotSize);
             }

	XCopyArea(display, doubleBuffer, win, gc, 0, 0, plotSize * extent.width, plotSize * extent.height, 
			plotSize * (position.x * extent.width), plotSize * (position.y * extent.height));		
}

/**
*Author: Tran Van Hoang
*Date: 11-03-2015
*Desciption: This function displays one cell with certain color on picture. Showing the density of cell by adding density value to cell's color.
*/
void showCellDensity(CellArray  myCell, Display *display, Colormap colormap, Window win, GC gc, int plotSize, int density)
{
	CellPosition position;
   	CellPosition maxPosition= {0,0};
   	CellImage image;
   	ImageExtent extent;
	char  colorName[100];
   	XColor color;
   	int xOffset, yOffset, pixelIndex;// offset;
	int depth;
   	struct RGBPixel pixel;
	Pixmap doubleBuffer;
        position = myCell.position; /* x,y offset in the view */
        maxPos(position, &maxPosition);
        image = myCell.image;
        extent = image.extent; /* width and height of the cell */
	depth = DefaultDepth(display, DefaultScreen(display));
	doubleBuffer = XCreatePixmap(display, 
				DefaultRootWindow(display), 
				extent.width * plotSize, 
				extent.height * plotSize, 
				depth);
	
        for (yOffset = 0; yOffset < extent.height ; yOffset ++)
            for (xOffset = 0; xOffset < extent.width ; xOffset ++)
            {
		//if (cellColor == -1)
		{
	 		pixelIndex = yOffset * extent.width + xOffset;
                	pixel = image.pixelArray[pixelIndex];
                	sprintf(colorName, "#%2.2x%2.2x%2.2x", pixel.red - density, pixel.green - density, pixel.blue - density);
                	XParseColor(display, colormap, colorName, &color);
                	XAllocColor(display, colormap, &color);
                	XSetForeground(display, gc, color.pixel);
		}
		//else
		{
	   	  	//XSetForeground(display, gc, cellColor);
		}
                XFillRectangle(display, doubleBuffer, gc, 
			   plotSize * xOffset, 
			   plotSize * yOffset , 
			   plotSize, 
			   plotSize);
             }

	XCopyArea(display, doubleBuffer, win, gc, 0, 0, plotSize * extent.width, plotSize * extent.height, 
			plotSize * (position.x * extent.width), plotSize * (position.y * extent.height));		
}

/**
*This functions display one cell with certain color on picture. If cellColor=0, show the orginal color of the image.
*/
void showCell(CellArray  myCell, Display *display, Colormap colormap, Window win, GC gc, int plotSize, long cellColor)
{
	CellPosition position;
   	CellPosition maxPosition= {0,0};
   	CellImage image;
   	ImageExtent extent;
	char  colorName[100] ;
   	XColor color;
   	int xOffset, yOffset, pixelIndex;// offset;
	int depth;
   	struct RGBPixel pixel;
	Pixmap doubleBuffer;
        position = myCell.position; /* x,y offset in the view */
        maxPos(position, &maxPosition);
        image = myCell.image;
        extent = image.extent; /* width and height of the cell */
	depth = DefaultDepth(display, DefaultScreen(display));
	doubleBuffer = XCreatePixmap(display, 
				DefaultRootWindow(display), 
				extent.width * plotSize, 
				extent.height * plotSize, 
				depth);
	
        for (yOffset = 0; yOffset < extent.height ; yOffset ++)
            for (xOffset = 0; xOffset < extent.width ; xOffset ++)
            {
		if (cellColor == -1)
		{
	 		pixelIndex = yOffset * extent.width + xOffset;
                	pixel = image.pixelArray[pixelIndex];
                	sprintf(colorName, "#%2.2x%2.2x%2.2x",pixel.red,pixel.green,pixel.blue);
                	XParseColor(display, colormap, colorName, &color);
                	XAllocColor(display, colormap, &color);
                	XSetForeground(display, gc, color.pixel);
		}
		else
		{
	   	  	XSetForeground(display, gc, cellColor);
		}
                XFillRectangle(display, doubleBuffer, gc, 
			   plotSize * xOffset, 
			   plotSize * yOffset , 
			   plotSize, 
			   plotSize);
             }

	XCopyArea(display, doubleBuffer, win, gc, 0, 0, plotSize * extent.width, plotSize * extent.height, 
			plotSize * (position.x * extent.width), plotSize * (position.y * extent.height));		
}

void showCells(CellArray  *myCells, Display *display, Colormap colormap, Window win, GC gc, int plotSize, int *state, int node_number)
{
	CellPosition position;
   	CellPosition maxPosition= {0,0};
   	CellImage image;
   	CellArray cellArray;
   	cachedImage cacheEntry;
   	char  colorName[100] ;
   	XColor color;
   	ImageExtent extent;
   	int i, xOffset, yOffset, pixelIndex;// offset;
	int depth;
   	int screenNumber = DefaultScreen(display);   
   	unsigned long black = BlackPixel(display, screenNumber);
   	struct RGBPixel pixel;
	Pixmap doubleBuffer;

   	for (i = 0; i < node_number; i++)
   	{	    
            cellArray = myCells[i];
            position = cellArray.position; /* x,y offset in the view */
            cacheEntry.index = i;
            cacheEntry.position = position;
            maxPos(position, &maxPosition);
            image = cellArray.image;
            extent = image.extent; /* width and height of the cell */
	    depth = DefaultDepth(display, DefaultScreen(display));
	    doubleBuffer = XCreatePixmap(display, 
					DefaultRootWindow(display), 
					extent.width * plotSize, 
					extent.height * plotSize, 
					depth);

            for (yOffset = 0; yOffset < extent.height ; yOffset ++)
                 for (xOffset = 0; xOffset < extent.width ; xOffset ++)
                 {
	    	    if (state[i] == 1)
	            {
                	pixelIndex = yOffset * extent.width + xOffset;
                	pixel = image.pixelArray[pixelIndex];
                	sprintf(colorName, "#%2.2x%2.2x%2.2x",pixel.red,pixel.green,pixel.blue);
                	XParseColor(display, colormap, colorName, &color);
                	XAllocColor(display, colormap, &color);
                	XSetForeground(display, gc, color.pixel);
	            } 
	            else
	            {
		    	XSetForeground(display, gc, black);
	       	    }

                    XFillRectangle(display, doubleBuffer, gc, 
				   plotSize * xOffset, 
				   plotSize * yOffset , 
				   plotSize, 
				   plotSize);
                }

		XCopyArea(display, doubleBuffer, win, gc, 0, 0, plotSize * extent.width, plotSize * extent.height, 
			plotSize * (position.x * extent.width), plotSize * (position.y * extent.height));		
                imageCache[i] = cacheEntry;	
   }
}


